import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { aesReq, getQueryString } from './utils/utils';
import { GOVERNMENT_LINK } from './utils/government-link';
import { MessageService } from './service/message.service';
import { LoginService } from './service/login.service';
import { UserInfoData, UserInfoService } from './service/user-info/user-info.service';
import { first, firstValueFrom } from 'rxjs';
import { DY_FORM_LINK } from './utils/dy-form-link';
import { CommonService } from './service/common/common.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  
  title = 'tc-web';
  constructor(
    private route: ActivatedRoute,
    private router: Router,
    private message: MessageService,
    private loginService: LoginService,
    private userInfo: UserInfoService,
    private common: CommonService
  ){}

  authCode: any
  type: any
  dyAuthFormId: any
  redirectUrl = ''
  ngOnInit(): void {
    const id = getQueryString('id')
    this.authCode = getQueryString('code')
    this.type = getQueryString('type')
    this.dyAuthFormId = getQueryString('dyAuthFormId')
    this.type = '0'

    // 电子证照 实名认证
    if(this.dyAuthFormId){
      console.log(this.dyAuthFormId)
      let func = DY_FORM_LINK.find((_: any) => _.setId === this.dyAuthFormId)
      if(!func){
        return
      }
      window.open(location.origin + func.link + '&authCode='+this.authCode, '_self')
      return
    }

    const func = GOVERNMENT_LINK.find((_: any) => _.id === id)
    if(func){
      // window.open(location.origin + func.link, '_self')
      this.redirectUrl = location.origin + func.link
    }

    if(this.authCode){
      this.loginByAuthCode()
    }else{

    }
  }

  async loginByAuthCode(){
    const res = await firstValueFrom(this.loginService._loginByAuthCode(this.authCode))
    console.log('====================', this.type)
    this.loginService.afterLogin(res, this.redirectUrl)
  }

  
}
